<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList,teamCservlets.PersonBean" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Team C Defect System</title>
<style>
td {
    padding: 5px;
}
</style>
</head>

<%-- 
First, get all of the session and request data this jsp requires.  Make sure all
the data is available before building the display, to avoid any null pointer exceptions.
--%>
<jsp:useBean id="user" type="teamCservlets.PersonBean" scope="session"/>
<%
ArrayList<PersonBean> people = (ArrayList<PersonBean>)session.getAttribute("personList");
String subjectLine = (String)request.getAttribute("subjectLine");
String emailClosingText = (String)request.getAttribute("emailClosingText");
String replyTo = (String)request.getAttribute("replyTo");

boolean error = false;

// The "user" attribute identifies the currently logged-in user, if any.  If it's not a valid
// user, force the user to login.
if ((user == null) || (user.getId() == -1)) { 
	// No valid user object in the session, so the user must login
	response.sendRedirect("login.jsp");
	error = true;
} 
// List of users, used to populate the To: combo box.
if (people == null) {
	out.println("people is null");
	error = true;
}
// Email-specific data.
if (subjectLine == null || emailClosingText == null || replyTo == null) {
	out.println("One or more email-specific fields is null (subjectLine, emailText, replyTo)");
	error = true;
}
if (!error) {
%>

<body BGCOLOR="#FDF5E6">
	<H1 ALIGN="CENTER">Team C Defect System</H1>
	<p style="font-size:150%">Email Defect Information</p>
	<br>
	<form action="defectactions">
	<table>
	
	<tr><td>To:</td>
		<td><select name="sendTo" width="100%">	
	<%
	for (PersonBean person : people) {
	%>
			<option value="<%= person.getEmailAddress() %>"><%= person.getFirstAndLastName() %>
	<%
	} // end of for loop 
	%>
	</select></td></tr>
	
	<tr><td>Subject:</td><td><%= subjectLine %></td>
	</tr>
	
	</table>
	<br>
	<table>
	<tr>
	<td>Enter your message about this defect:</td>
	</tr>
	<tr>
	<td align="right" colspan="2" style="width:80"><textarea name="message" rows=10 cols=60 ></textarea></td>
	</tr>
	<tr>
		<td colspan="2" align="center">
		<input type="submit" name="btnSendEmail" value="Send">
		<input type="submit" name="btnGoToHomepage" value="Cancel">
	</tr>
	</table>
	
	<%-- Hidden fields provide request parameters back to the servlet --%>
	<input type="hidden" name="subjectLine" value="<%=subjectLine%>">
	<input type="hidden" name="emailClosingText" value="<%=emailClosingText%>">
	<input type="hidden" name="replyTo" value="<%=replyTo%>">
	
	</form>
</body>

<% } // end if (!error)
%>

</html>
